package com.atguigu.interview.juc;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Jungle
 * @create 2023-09-05 0:02
 */
public class LockSupportDemo {
    static Object objectLock = new Object();
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) {

        new HashMap<>().put("1", "2");
        Thread threadA = new Thread(() -> {

            //@formatter:off  线程暂停一会儿
            try { TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace();}
            //@formatter:on
            System.out.println(Thread.currentThread().getName() + "\t come in" + System.currentTimeMillis());
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t ======被唤醒" + System.currentTimeMillis());
        }, "A");
        threadA.start();


        new Thread(() -> {
            //@formatter:off  线程暂停一会儿
            //try { TimeUnit.SECONDS.sleep( 1 ); } catch (InterruptedException e) { e.printStackTrace();}
            //@formatter:on
            LockSupport.unpark(threadA);
            System.out.println(Thread.currentThread().getName() + "\t 通知");
        }, "B").start();


    }

    /**
     * reentrantLock通过condition的通知唤醒
     */
    private static void reentrantLockAwaitSignal() {
        new Thread(() -> {
            //@formatter:off  线程暂停一会儿
            try { TimeUnit.SECONDS.sleep( 1 ); } catch (InterruptedException e) { e.printStackTrace();}
            //@formatter:on
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t ======被唤醒");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "A").start();
        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t 通知");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "B").start();
    }

    /**
     * synchronized通知唤醒
     */
    private static void syncWaitNotify() {
        new Thread(() -> {

            //@formatter:off  线程暂停一会儿
            try { TimeUnit.SECONDS.sleep( 1 ); } catch (InterruptedException e) { e.printStackTrace();}
            //@formatter:on
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "\t =====come in");
                try {
                    objectLock.wait(); //wait() notify方法必须放到代码块中
                    System.out.println(Thread.currentThread().getName() + "\t ====被唤醒");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "A").start();


        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
            }
            System.out.println(Thread.currentThread().getName() + "\t =====通知");
        }, "B").start();
    }
}
